package components {
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Circ;
	import com.greensock.easing.Linear;
	import com.greensock.easing.Quart;
	import com.limetalks.data.Stream;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Mouse;
	import flash.utils.Timer;
	
	import mx.controls.Alert;
	
	public class StreamMC extends MovieClip {
		
		private var _cx:Number;
		private var _cy:Number;
		private var _cz:Number;
		public var per:Number;
		
		private var time:int = 0;
		private var maxTime:int = 5;
		
		private var _this:MovieClip;
		
		private var _tf:TextField;
		private var _atf:TextField;
		private var _back:Sprite;
		
		private var greyMc:MovieClip;
		
		private var loader:Loader;
		
		private var _hicolor:Number = 0xeeeeee;
		
		public var isDragged:Boolean = false;
		
		private var _root:MovieClip;
		
		public var stream:Stream;
		
		public var hasImage:Boolean = false;
		
		public function StreamMC(parentMC:MovieClip, stream:Stream) {
			
			//TODO: load image from server via Loader
			/*loader = new Loader();
			
			var randomNum:Number = Math.random()*4;
			if(randomNum <= 1)
				pictURL = "http://i4.ytimg.com/vi/KmGCc6R-pbo/default.jpg";
			else if(randomNum <= 2)
				pictURL = "http://i1.ytimg.com/vi/Xz2F2lkBPvU/default.jpg";
			else if(randomNum <= 3)
				pictURL = "http://i3.ytimg.com/vi/6jam0lz41gk/default.jpg";
			else if(randomNum <= 4)
				pictURL = "http://i3.ytimg.com/vi/Zc-xdUvVFjo/default.jpg";
			var pictURLReq:URLRequest = new URLRequest(pictURL); 
			loader.load(pictURLReq);
			this.addChild(loader);*/
			//loader.width = loader.height = 50;
			
				
			
			this.stream = stream;
			_root = parentMC;
			_this = this;
			
			_tf = new TextField();
			_tf.autoSize = TextFieldAutoSize.LEFT;
			_tf.selectable = false;
			_tf.text = stream.name;
			_tf.x = 0;
			_tf.y = -30;
			
			if(stream.image != "")
				hasImage = true;
			
			if(hasImage) {
				var pictURL:String = "http://93.84.121.112:5080/LimeTalksRed5/images/stream/" + stream.image;
				var pictURLReq:URLRequest = new URLRequest(pictURL);
				loader = new Loader();
				loader.load(pictURLReq);
				this.addChild(loader);
			}
			
			var textForamt:TextFormat = new TextFormat();
			textForamt.font = "Arial";
			textForamt.bold = true;
			textForamt.color = 0x000000;
			textForamt.size = 16;
			_tf.defaultTextFormat = textForamt;
			_tf.setTextFormat(textForamt);
			
			addChild(_tf);
			
			_atf = new TextField();
			_atf.autoSize = TextFieldAutoSize.LEFT;
			_atf.selectable = false;
			_atf.text = ""; //broadcast.author;
			_atf.x = 10;
			_atf.y = 75;
			
			greyMc = new MovieClip();
			greyMc.graphics.beginFill(0xcccccc);
			//greyMc.graphics.lineStyle(1, 0xcccccc);
			greyMc.graphics.drawRect(0, 0, 120, 20);
			greyMc.graphics.endFill();
			greyMc.alpha = 0;
			greyMc.y = 0;
			greyMc.x = 0;
			//addChild(greyMc);
			
			var format:TextFormat = new TextFormat();
			format.font = "Arial";
			//format.bold = true;
			format.color = 0x00bb00;
			format.size = 12;
			_atf.defaultTextFormat = format;
			_atf.setTextFormat(format);
			//_atf.text = stream.author;
			//addChild(_atf);
			
			// create the back
			_back = new Sprite();
			_back.graphics.beginFill(_hicolor);
			_back.graphics.lineStyle(0, 0xdddddd);
			//_back.graphics.drawRect(0, 0, _tf.textWidth+40, _tf.textHeight+10);
			_back.graphics.drawRect(0, 0, 130, 100);
			_back.graphics.endFill();
			addChildAt(_back, 0);
			_back.x = -5;//-( _tf.textWidth/2 ) - 10;
			_back.y = -5;//-( _tf.textHeight/2 ) - 2;
			//_back.visible = false;
			
			
			addEventListener(Event.ENTER_FRAME, function():void {
				
				//var zi:int = _this.transform.getRelativeMatrix3D(_root).position.z;
				//_atf.text = new String("scale: " + per);
			});
			
			addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			
			//addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
			addEventListener(MouseEvent.MOUSE_UP, mouseReleasedHandler);
		}
		
		private function mouseReleasedHandler(e:MouseEvent):void {
			
			(Sphere(_root)).streamSelected(stream);
		}
		
		private function mouseOverHandler( e:MouseEvent ):void {
			_back.visible = true;
			
			if(hasImage) {
				loader.x -= 10;
				loader.y -= 10;
				loader.scaleX = loader.scaleY = 1.2;
			}
			
			//_atf.text = stream.author;
			//_tf.textColor = _hicolor;
			//_active = true;
			greyMc.alpha = 0.5;
			
			_back.x = -15;
			_back.y = -15;
			_back.scaleX = _back.scaleY = 1.19;
			
			_tf.y = -40;
			_tf.scaleX = _tf.scaleY = 1.1;
		}
		
		private function mouseOutHandler( e:MouseEvent ):void {
			//_back.visible = false;
			if(hasImage) {
				loader.x += 10;
				loader.y += 10;
				loader.scaleX = loader.scaleY = 1;
			}
				//_atf.text = "";
			//_tf.textColor = _color;
			//_active = false;
			greyMc.alpha = 0;
			
			_back.x = -5;
			_back.y = -5;
			_back.scaleX = _back.scaleY = 1;
			
			_tf.y = -30;
			_tf.scaleX = _tf.scaleY = 1;
		}
		
		public function remove():void {
			
			
			TweenLite.to(this, 2, {alpha:0, 
				onComplete: function():void {
					_root.removeChild(_this);
					//_root.mcList.splice(index,1);
				}
			});
			
			//_root.removeChild(_root.mcList[index]);
			//_root.mcList.splice(index,1);
			
			/*var t = new Tween(this, "alpha", Regular.easeIn, alpha, 0, 3, true);
			t.addEventListener(TweenEvent.MOTION_FINISH, function(e:TweenEvent) {
			_root.removeChild(_root.mcList[index]);
			_root.mcList.splice(index,1);
			
			});
			*/
			//_root.removeChild(this);
		}
		
		public function set cx(n:Number):void {_cx = n;}
		public function set cy(n:Number):void {_cy = n;}
		public function set cz(n:Number):void {_cz = n;}
		public function get cx():Number {return _cx;}
		public function get cy():Number {return _cy;}
		public function get cz():Number {return _cz;}
	}
	
}
